package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lzx.iteam.AttendanceSubClassActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.AttendanceClassData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 11月 24日
 * Time: 下午4:39
 *
 * @QQ : 1234567890
 * **********************************
 */
public class AttendanceSubClassAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<AttendanceClassData> mDatas;

    public AttendanceSubClassAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void bindData(ArrayList<AttendanceClassData> mDatas){
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mDatas == null ? 0:mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        AttendanceClassData classData = mDatas.get(position);
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.at_sub_item_layout,null);
            holder.name = (TextView) convertView.findViewById(R.id.tv_at_item_sub_name);
            holder.time = (TextView) convertView.findViewById(R.id.tv_at_item_sub_time);
            holder.layoutCreate = (RelativeLayout) convertView.findViewById(R.id.rl_at_item_create_shift);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == getCount() -1){
            holder.layoutCreate.setVisibility(View.VISIBLE);
        }else{
            holder.layoutCreate.setVisibility(View.GONE);
        }
        holder.name.setText(classData.getSub_name());
        if (classData.getIs_quartic() == 0){
            holder.time.setText(classData.getStart_work_time() + "    " + checkTime(classData.getEnd_work_time()));
        }else{
            if (classData.getNoon_end_time().contains("-")){
                holder.time.setText(classData.getStart_work_time() + " - " + checkTime(classData.getNoon_end_time()) + "\n" +checkTime(classData.getNoon_start_time()) + " - " +checkTime(classData.getEnd_work_time()));
            } else{
                holder.time.setText(classData.getStart_work_time() + " - " + checkTime(classData.getNoon_end_time()) + "  " +checkTime(classData.getNoon_start_time()) + " - " +checkTime(classData.getEnd_work_time()));
            }
        }

        holder.layoutCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AttendanceSubClassActivity)mContext).createNewShift();
            }
        });
        return convertView;
    }

    private String checkTime(String time){
        if (time.contains("-")){
            return time.replace("-","次日");
        }else{
            return time;
        }
    }

    class ViewHolder{
        private TextView name,time;
        private RelativeLayout layoutCreate;
    }
}
